//package com.telit.firecommand.service.impl;
//
//import cn.hutool.core.map.MapUtil;
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.telit.common.constant.DictTypeConstants;
//import com.telit.common.entity.PageData;
//import com.telit.common.exception.ForestBaseException;
//import com.telit.common.util.DateUtils;
//import com.telit.common.vo.user.Result;
//import com.telit.dispose.feign.RemoteDisposeService;
//import com.telit.firecommand.dao.*;
//import com.telit.firecommand.dto.CommHistoryFireDTO;
//import com.telit.firecommand.dto.FireBurnedAreaDTO;
//import com.telit.firecommand.entity.*;
//import com.telit.firecommand.enums.FirelevelStatus;
//import com.telit.firecommand.feign.AlarmsFeignClient;
//import com.telit.firecommand.feign.PatrolFeignClient;
//import com.telit.firecommand.service.FileUploadLogService;
//import com.telit.firecommand.service.PostDisasterManagementService;
//import com.telit.firecommand.vo.*;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.CollectionUtils;
//
//import java.util.*;
//
//import static com.telit.common.service.ParamsCheckService.*;
//
//
///**
// * Create by Chencf 2022/10/01
// */
//
//@Slf4j
//@Service
//public class PostDisasterManagementServiceImpl implements PostDisasterManagementService {
//
//    @Autowired
//    private CommHistoryFireDao commHistoryFireDao;
//    @Autowired
//    private ForestLandInformationDao forestLandInformationDao;
//
//    @Autowired
//    private FireAlarmsDao fireAlarmsDao;
//
//    @Autowired
//    private CommFireHandleDao commFireHandleDao;
//
//    @Autowired
//    private FireWeatherInfoDao fireWeatherInfoDao;
//
//    @Autowired
//    private GeoXzqhCunDao geoXzqhCunDao;
//
//    @Autowired
//    private AlarmsFeignClient alarmsFeignClient;
//
//    @Autowired
//    private FileUploadLogDao fileUploadLogDao;
//
//
//    @Autowired
//    private PatrolFeignClient patrolFeignClient;
//
//    @Autowired
//    private RemoteDisposeService disposeFeignClient;
//    @Autowired
//    private CommunicateRecordService communicateRecordService;
//
//    @Autowired
//    private FileUploadLogService fileUploadLogService;
//
//    @Autowired
//    private SysOperationLogDAO sysOperationLogDAO;
//
//    @Autowired
//    private FireQueryDao fireQueryDao;
//
//
//
//    @Override
//    public Object queryAllHistoryFire(CommHistoryFireDTO commHistoryFireDTO) {
//        if (commHistoryFireDTO.getPage() == null) {
//            commHistoryFireDTO.setPage(1);
//        }
//        if (commHistoryFireDTO.getRows() == null) {
//            commHistoryFireDTO.setRows(Integer.MAX_VALUE);
//        }
//        IPage<CommHistorySimpFireVO> commHistorySimpFireVOIPage = new Page<>(commHistoryFireDTO.getPage(),
//                commHistoryFireDTO.getRows());
//        if (StringUtils.isNotBlank(commHistoryFireDTO.getDistrictCode())) {
//            commHistoryFireDTO.setDistrictCode(commHistoryFireDTO.getDistrictCode() + "%");
//        }
//        if (commHistoryFireDTO.getStatus() == null) {
//            commHistoryFireDTO.setStatus(4);
//        }
//        IPage<CommHistorySimpFireVO> pages = commHistoryFireDao.queryAllHistoryFire(commHistorySimpFireVOIPage,
//                commHistoryFireDTO);
//        List<CommHistorySimpFireVO> records = pages.getRecords();
//        for (CommHistorySimpFireVO commHistorySimpFireVO : records) {
//            //根据行政区划代码查询地址
//            if (commHistorySimpFireVO.getDistrictCode() != null) {
//                commHistorySimpFireVO.setDistrictCodeName(
//                        geoXzqhCunDao.queryNameByCode(commHistorySimpFireVO.getDistrictCode(),
//                                commHistorySimpFireVO.getDistrictCode().length()));
//            }
//            //commHistorySimpFireVO.setDistrictCodeName(commHistorySimpFireVO.getAdress().substring(0,6));
//            if (StringUtils.isNotBlank(commHistorySimpFireVO.getFireLevel())) {
//                commHistorySimpFireVO.setFireLevelStr(
//                        FirelevelStatus.code2Str(Integer.parseInt(commHistorySimpFireVO.getFireLevel())));
//            }
//        }
//        pages.setRecords(records);
//        PageData<Object> pageData = new PageData<>();
//        pageData.setDatas(pages.getRecords());
//        pageData.setCount((int) pages.getTotal());
//        pageData.setCurrent((int) pages.getCurrent());
//        pageData.setSize((int) pages.getSize());
//        return pageData;
//    }
//
//
//    @Override
//    public void addHistoryFire(CommHistoryFire commHistoryFire) {
//        int i = commHistoryFireDao.insert(commHistoryFire);
//    }
//
//    //修改历史火情表数据
//    @Override
//    public void updateHistoryFire(CommHistoryFire commHistoryFire) {
//        //修改新增
//        //step1 先查询历史表看是否有记录
////        QueryWrapper<CommHistoryFire> wrapper = new QueryWrapper<>();
////        wrapper.eq("", commHistoryFire.getId());
//        //     判断comm_history_fire表里面有没有火情对应火险等级
//
//        if (commHistoryFire.getCommFireHandle() != null) {
//            //修改数据
//            CommFireHandle commFireHandle = commFireHandleDao.selectByfireId(commHistoryFire.getFireId());
//            if (commFireHandle != null) {
//                CommFireHandle webData = commHistoryFire.getCommFireHandle();
//                webData.setFireId(commHistoryFire.getFireId());
//                webData.setFireJudgmentLevel(commHistoryFire.getFireLevel());
//                webData.setId(commFireHandle.getId());
//                webData.setUpdateTime(new Date());
//                commFireHandleDao.updateById(webData);
//            } else {
//                CommFireHandle entity = commHistoryFire.getCommFireHandle();
//                entity.setFireId(commHistoryFire.getFireId());
//                entity.setFireJudgmentLevel(commHistoryFire.getFireLevel());
//                entity.setCreateTime(new Date());
//                entity.setUpdateTime(new Date());
//                commFireHandleDao.insert(entity);
//            }
//        }
//
//        Long aLong = commHistoryFireDao.selecthFire(commHistoryFire.getFireId());
//        // CommHistoryFire exist = commHistoryFireDao.selectInfoByFireId(commHistoryFire.getFireId());
//        if (aLong != null) {
//            commHistoryFire.setId(aLong);
//            commHistoryFireDao.updateByPrimaryKeySelective(commHistoryFire);
//        } else {
//            commHistoryFireDao.insert(commHistoryFire);
//        }
//    }
//
//    //根据火情id查询历史火情信息(全部信息)
//    @Override
//    public CommHistoryFireVO queryAllHistoryFireByFireId(String fireId) {
//
//        CommHistoryFireVO commHistoryFireVO = commHistoryFireDao.queryAllHistoryFireByFireId(fireId);
//        if (commHistoryFireVO == null) {
//            commHistoryFireVO = new CommHistoryFireVO();
//            //补全火情的基本信息
//            FireAlarmsVO fireAlarmsVO = commHistoryFireDao.selectFindAlarm(fireId);
//            commHistoryFireVO.setDistrictCodeName(
//                    geoXzqhCunDao.queryNameByCode(fireAlarmsVO.getAreaCode(), fireAlarmsVO.getAreaCode().length()));
//            commHistoryFireVO.setDistrictCode(fireAlarmsVO.getAreaCode());
//            commHistoryFireVO.setStartTime(fireAlarmsVO.getStartTime());
//            commHistoryFireVO.setPlaceName(fireAlarmsVO.getPlaceName());
//        }
//        if (commHistoryFireVO != null) {
//            if (commHistoryFireVO.getDistrictCode() != null) {
//                commHistoryFireVO.setDistrictCodeName(geoXzqhCunDao.queryNameByCode(commHistoryFireVO.getDistrictCode(),
//                        commHistoryFireVO.getDistrictCode().length()));
//            }
//            // commHistoryFireVO.setDistrictCodeName(commHistoryFireVO.getPlaceName().substring(0, 6));
//            if (StringUtils.isNotBlank(commHistoryFireVO.getFireLevel())) {
//                commHistoryFireVO.setFireLevelStr(
//                        FirelevelStatus.code2Str(Integer.parseInt(commHistoryFireVO.getFireLevel())));
//            }
//        }
//
//        //林板信息
//        ForestLandInformation forestLandInformation = forestLandInformationDao.forestLandInformation(fireId);
//        commHistoryFireVO.setForestLandInformation(forestLandInformation);
//
//        //指挥信息
//        CommFireHandle commFireHandle = commFireHandleDao.selectByfireId(fireId);
//        commHistoryFireVO.setCommFireHandle(commFireHandle);
//        //查询天气信息
//        FireWeatherInfo fireWeatherInfo = fireWeatherInfoDao.queryByFireId(fireId);
//        commHistoryFireVO.setFireWeatherInfo(fireWeatherInfo);
//
//
//        return commHistoryFireVO;
//    }
//
//    //历史火情回溯接口i
//    @Override
//    public FireTraceVO findFireTrace(String fireId) {
//
//        FireTraceVO fireTraceVO = new FireTraceVO();
//
//        //查询火情回溯
//        List<FireStatusTraceVO> list = sysOperationLogDAO.fireOperatorLogByFireId(fireId);
//        //step1 查询历史火情状态回溯
//        //List<FireStatusTraceVO> fireStatusTrace = fireAlarmsDao.findFireStatusTrace(fireId);
//        fireTraceVO.setFireStatusTraceVOS(list);
//
////        List<CommCommunicateRecord> commCommunicateRecords = communicateRecordService.listByFireId(fireId);
////        fireTraceVO.setCommCommunicateRecords(commCommunicateRecords);
//        return fireTraceVO;
//    }
//
//
//    @Override
//    public Map<String, List<MultimediaVO>> queryMediaById(String fireId) {
//        Map<String, List<MultimediaVO>> resultMap = new HashMap<>();
//        List<MultimediaVO> imgList = new ArrayList<>();
//        List<MultimediaVO> audioList = new ArrayList<>();
//        List<MultimediaVO> videoList = new ArrayList<>();
//        List<MultimediaVO> docList = new ArrayList<>();
//        // 查询火情表的多媒体信息
//        FireQueryAllListVO fireDetailByFireId = fireQueryDao.findFireDetailByFireId(fireId);
////        DataReturn dataReturn = alarmsFeignClient.qryAlarmsInfo(fireId);
////        if (!ResultCode.SUCCESS.getCode().equals(dataReturn.getCode())
////                && !FireConstant.ALARMS_SUCCESS_CODE.equals(dataReturn.getCode())) {
////            throw new ForestBaseException(dataReturn.getErrorMsg());
////        }
////        FireAlarmsDTO fireAlarmsDTO = JSON.parseObject(JSON.toJSONString(dataReturn.getData()), FireAlarmsDTO.class);
//        String sourceTypeName = fireAlarmsDao.selectOneByDictType(DictTypeConstants.DICT_TYPE_FIRE_ALARM_RESOURCE,
//                fireDetailByFireId.getSourceType() + "");
//        if (StringUtils.isNoneBlank(fireDetailByFireId.getPicUrls())) {
//            //按照，分割
//            List<String> fireImgList = com.telit.common.util.StringUtils.convertStringListBy(
//                    fireDetailByFireId.getPicUrls());
//            if (!CollectionUtils.isEmpty(fireImgList)) {
//                fireImgList.forEach(fireImg -> {
//                    // 封装数据
//                    MultimediaVO multimediaDTO = new MultimediaVO(fireImg, sourceTypeName, DateUtils.getDate(fireDetailByFireId.getStartTime(),"yyyy-MM-dd"),
//                           null);
//                    // multimediaDTO.setMediaUrl(MinioUtils.previewImg(fireImg));
//                    // 判断是哪个list里面的数据
//                    String fileType = fireImg.substring(fireImg.lastIndexOf(".") + 1, fireImg.length());
//                    classification(fileType, multimediaDTO, imgList, videoList, audioList, docList);
//                });
//            }
//        }
//
//        // 查询上传图片记录表中多媒体信息
//        List<FileUploadLog> fileUploadLogList = fileUploadLogDao.listByFireId(fireId);
//        if (!CollectionUtils.isEmpty(fileUploadLogList)) {
//            fileUploadLogList.forEach(fileUploadLog -> {
//                if (fileUploadLog != null && StringUtils.isNotBlank(fileUploadLog.getFilePath())) {
//                    String filePath = fileUploadLog.getFilePath();
//                    String sourceName = sourceTypeName;
//                    if (StringUtils.isNotEmpty(fileUploadLog.getAlarmResource())) {
//                        sourceName = fireAlarmsDao.selectOneByDictType(DictTypeConstants.DICT_TYPE_FIRE_ALARM_RESOURCE,
//                                fileUploadLog.getAlarmResource() + "");
//                    }
//                    // 封装数据
////                    MultimediaVO multimediaVO = new MultimediaVO(MinioUtils.previewImg(fileUploadLog.getFilePath()),
////                            sourceName, fileUploadLog.getUploadTime(), fileUploadLog.getUploader(),
////                            fileUploadLog.getFileName());
//                    //TODO:v1.1.0 预览图片
//
//                    MultimediaVO multimediaVO = new MultimediaVO(null,
//                            sourceName, fileUploadLog.getUploadTime(), fileUploadLog.getUploader(),
//                            fileUploadLog.getFileName());
//                    multimediaVO.setMediaUrl(fileUploadLog.getFilePath());
//                    String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
//                    classification(fileType, multimediaVO, imgList, videoList, audioList, docList);
//                }
//            });
//        }
//        resultMap.put("audioList", audioList);
//        resultMap.put("imgList", imgList);
//        resultMap.put("videoList", videoList);
//        resultMap.put("docList", docList);
//        return resultMap;
//    }
//
//    //修改火烧迹地
//    @Override
//    public void updateBurnedArea(FireBurnedAreaDTO fireBurnedAreaDTO) {
//        //查询是否已经存在历史火情
//        Long aLong = commHistoryFireDao.selecthFire(fireBurnedAreaDTO.getFireId());
//        if (aLong != null) {
//            fireBurnedAreaDTO.setId(aLong);
//            commHistoryFireDao.updateBurnedArea(fireBurnedAreaDTO);
//        } else {
//            CommHistoryFire commHistoryFire = new CommHistoryFire();
//            commHistoryFire.setFireId(fireBurnedAreaDTO.getFireId());
//            commHistoryFire.setFirePoint(fireBurnedAreaDTO.getFirePoint());
//            commHistoryFire.setFireArea(fireBurnedAreaDTO.getFireArea());
//            commHistoryFireDao.insertSelective(commHistoryFire);
//        }
//
//    }
//
//    //查询火烧迹地信息
//    @Override
//    public FireBurnedAreaVO findBurnedArea(String fireId) {
//        FireBurnedAreaVO burnedArea = commHistoryFireDao.findBurnedArea(fireId);
//        if (StringUtils.isNotBlank(burnedArea.getFireLevel())) {
//            burnedArea.setFireLevelStr(FirelevelStatus.code2Str(Integer.parseInt(burnedArea.getFireLevel())));
//        }
//        return burnedArea;
//    }
//
//    @Override
//    public List<FireTaskListVo> fireTaskList(String fireId) {
//        Result result = patrolFeignClient.fireTaskList(fireId, 1, 100);
//        if (!result.isSuccess()) {
//            throw new ForestBaseException(result.getCode(), null, result.getErrorMsg());
//        }
//        Map data = JSON.parseObject(JSON.toJSONString(result.getData()), Map.class);
//        List<FireTaskListVo> records = JSON.parseArray(MapUtil.getStr(data, "records"), FireTaskListVo.class);
//        return records;
//    }
//
//    public void classification(String fileType, MultimediaVO multimediaDTO, List<MultimediaVO> imgList,
//                               List<MultimediaVO> videoList, List<MultimediaVO> audioList, List<MultimediaVO> docList) {
//        if (FILE_TYPE_IMAGE_LIST.contains(fileType) || fileType.length() > 10) {
//            imgList.add(multimediaDTO);
//        } else if (FILE_TYPE_VIDEO_LIST.contains(fileType)) {
//            videoList.add(multimediaDTO);
//        } else if (FILE_TYPE_AUDIO_LIST.contains(fileType)) {
//            audioList.add(multimediaDTO);
//        } else {
//            docList.add(multimediaDTO);
//        }
//    }
//
//    /**
//     * 保存火情图片
//     *
//     * @param fireFiles 文件
//     */
//    @Override
//    public void saveFireFiles(List<FileUploadLog> fireFiles) {
//        Date now = new Date();
//        fireFiles.forEach(log -> {
//            log.setUploadTime(now);
//            fileUploadLogService.add(log);
//        });
//    }
//
//}
